home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / tcpdumpb.zip / libpcap / pcap-spy.c < prev    next >
C/C++ Source or Header  |  1997-04-14  |  9KB  |  290 lines

  1. /*
  2.  * Copyright (c) 1993, 1994, 1995, 1996
  3.  *      The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static  char rcsid[] =
  23.     "@(#)$Header: pcap-snoop.c,v 1.15 96/07/15 00:48:52 leres Exp $ (LBL)";
  24. #endif
  25.  
  26. #define INCL_BASE
  27. #include <os2.h>
  28. #include <sys/types.h>
  29. #include <sys/param.h>
  30. #include <netdb.h>
  31. #include <sys/file.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/socket.h>
  34. #include <sys/time.h>
  35. #include <sys/timeb.h>
  36.  
  37. #include <net/if.h>
  38. #include <net/route.h>
  39. #include <netinet/in.h>
  40. #include <netinet/in_systm.h>
  41. #include <netinet/ip.h>
  42. #include <netinet/ip_icmp.h>
  43. #include <netinet/if_ether.h>
  44. #include <netinet/udp.h>
  45. #include <netinet/udp_var.h>
  46. #include <netinet/tcp.h>
  47. #include <ipspy.h>
  48.  
  49. #include <errno.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54.  
  55. #include "pcap-int.h"
  56.  
  57. #include "gnuc.h"
  58. #ifdef HAVE_OS_PROTO_H
  59. #include "os-proto.h"
  60. #endif
  61.  
  62. static UCHAR IpSpy_Device[IFNAMSIZ+1] = "";
  63. static ULONG IpSpy_Handle = 0;
  64. static USHORT IpSpy_OldMode;
  65. static int IpSpy_Cleanup_Installed = 0;
  66.  
  67. static void IpSpy_Cleanup (void)
  68. {
  69.         APIRET rc;
  70.  
  71.         if (IpSpy_Handle) {
  72.           if ((rc = IpSpy_Exit(IpSpy_Handle)) != RC_IPSPY_NOERROR)
  73.             fprintf(stderr, "IpSpyExit Error: %d\n", rc);
  74.  
  75.           if ((rc = IpSpy_SetReceiveMode(IpSpy_OldMode, IpSpy_Device, NULL)) != RC_IPSPY_NOERROR)
  76.             fprintf(stderr, "IpSpy_SetReceiveMode Error: %d\n", rc);
  77.  
  78.           fprintf(stderr, "IpSpy_Cleanup closed %s\n", IpSpy_Device);
  79.  
  80.           strcpy(IpSpy_Device, "");
  81.           IpSpy_Handle = 0;
  82.         }
  83. }
  84.  
  85. int
  86. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  87. {
  88.         register int datalen;
  89.         register int caplen;
  90.         register u_char *cp;
  91.  
  92.         APIRET rc;
  93.         USHORT usLength, usType;
  94.         ULONG  ulTimeStamp;
  95.         ULONG  ulTime;
  96.         USHORT usUnknown;
  97.  
  98.         usLength = p->bufsize;
  99.         if ((rc = IpSpy_ReadRaw(p->fd, (char *)p->buffer, &usLength, &usType, &ulTimeStamp, &usUnknown)) != RC_IPSPY_NOERROR) {
  100.           fprintf(stderr, "IpSpy Error IpSpy_ReadRaw(): %d\n", rc);
  101.           errno = 123; /* bogus error code */
  102.           sprintf(p->errbuf, "read: %s", pcap_strerror(errno));
  103.           return (-1);
  104.         }
  105.  
  106.         datalen = usLength;
  107.         caplen = usLength;
  108.         cp = p->buffer;
  109.  
  110.         if (p->fcode.bf_insns == NULL ||
  111.             bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
  112.                 struct pcap_pkthdr h;
  113.                 struct timeb tb;
  114.                 ++p->md.stat.ps_recv;
  115.                 _ftime(&tb);
  116.                 h.ts.tv_sec = tb.time;
  117.                 h.ts.tv_usec = tb.millitm * 1000;
  118.                 h.len = datalen;
  119.                 h.caplen = caplen;
  120.                 (*callback)(user, &h, cp);
  121.                 return (1);
  122.         }
  123.         return (0);
  124. }
  125.  
  126. int
  127. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  128. {
  129. #ifndef __EMX__
  130.         bzero((char *)rs, sizeof(*rs));
  131.         if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
  132.                 sprintf(p->errbuf, "SIOCRAWSTATS: %s", pcap_strerror(errno));
  133.                 return (-1);
  134.         }
  135. #endif
  136.  
  137.         p->md.stat.ps_drop = 0;
  138.  
  139. /*          rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
  140.             rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops; */
  141.  
  142.         *ps = p->md.stat;
  143.         return (0);
  144. }
  145.  
  146. pcap_t *
  147. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  148. {
  149.         pcap_t *p;
  150.  
  151.         APIRET rc;
  152.         UCHAR  *pVersion, **pIFs;
  153.         ULONG  ulHandle;
  154.         USHORT usOldMode;
  155.         UCHAR  *pSocketError;
  156.         ULONG  ulSocketError;
  157.         int i;
  158.  
  159.         p = (pcap_t *)malloc(sizeof(*p));
  160.         if (p == NULL) {
  161.                 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
  162.                 return (NULL);
  163.         }
  164.         bzero((char *)p, sizeof(*p));
  165.  
  166.         if ((rc = IpSpy_Version(&pVersion)) != RC_IPSPY_NOERROR) {
  167.           fprintf(stderr, "IpSpy Error IpSpy_Version(): %d\n", rc);
  168.           goto bad;
  169.         }
  170.         else
  171.           fprintf(stderr, "IpSpy Version: %s on device %s\n", pVersion, device);
  172.  
  173.         if ((rc = IpSpy_QueryReceiveMode(&usOldMode, NULL)) != RC_IPSPY_NOERROR) {
  174.           if (rc == RC_IPSPY_MODE_NOT_SUPPORTED)
  175.             fprintf(stderr, "IpSpy_QueryReceiveMode Error: Promiscuous mode not supported\n");
  176.           else if (rc == RC_IPSPY_CANNOT_OPEN_DRIVER)
  177.             fprintf(stderr, "IpSpy_QueryReceiveMode Error: Cannot open IPSPY.OS2\n");
  178.           else
  179.             fprintf(stderr, "IpSpy_QueryReceiveMode Error: %d\n", rc);
  180.           goto bad;
  181.         }
  182.  
  183.         if ((rc = IpSpy_QueryInterfaces(&pIFs)) != RC_IPSPY_NOERROR) {
  184.           printf("IpSpy_QueryInterfaces() Error: %d\n", rc);
  185.           goto bad;
  186.         } else {
  187.           fprintf(stderr, "supported interfaces:");
  188.           if (pIFs)
  189.             for (i=0; pIFs[i]; i++)
  190.               fprintf(stderr, " %s", pIFs[i]);
  191.           fprintf(stderr, "\n");
  192.         }
  193.  
  194.         if (promisc)
  195.           if ((rc = IpSpy_SetReceiveMode(DIRECTED_MODE | BROADCAST_MODE | PROMISCUOUS_MODE, device, NULL)) != RC_IPSPY_NOERROR) {
  196.             if (rc == RC_IPSPY_MODE_NOT_SUPPORTED)
  197.               fprintf(stderr, "Promiscuous mode not supported\n");
  198.             else if (rc == RC_IPSPY_CANNOT_OPEN_DRIVER)
  199.               fprintf(stderr, "IpSpy_SetReceiveMode Error: Cannot open IPSPY.OS2\n");
  200.             else
  201.               fprintf(stderr, "IpSpy_SetReceiveMode Error: %d\n", rc);
  202.             goto bad;
  203.           }
  204.  
  205.         if ((rc = IpSpy_Init(&ulHandle, device)) != RC_IPSPY_NOERROR) {
  206.           if (rc == RC_IPSPY_SOCKET_ERROR) {
  207.             IpSpy_GetLastSocketError(&ulSocketError, &pSocketError);
  208.             fprintf(stderr, "IpSpyInit SocketError: [%d] %s\n", ulSocketError, pSocketError);
  209.           }
  210.           else
  211.             fprintf(stderr, "IpSpy Error IpSpy_Init(): %d\n", rc);
  212.           goto bad;
  213.         }
  214.  
  215.         strcpy(IpSpy_Device, device);
  216.         IpSpy_Handle = ulHandle;
  217.         IpSpy_OldMode = usOldMode;
  218.         if (!IpSpy_Cleanup_Installed)
  219.           atexit(IpSpy_Cleanup);
  220.  
  221.         p->fd = ulHandle;
  222.         p->snapshot = snaplen; /* no snaplen for IpSpy */
  223.         /*
  224.          * XXX hack - map device name to link layer type
  225.          */
  226.         if (strncmp("lan", device, 3) == 0) {
  227.                 p->linktype = DLT_EN10MB;
  228.         } else if (strncmp("lo", device, 2) == 0) {
  229.                 p->linktype = DLT_NULL;
  230.         } else if (strncmp("sl", device, 2) == 0) {
  231.                 p->linktype = DLT_SLIP;
  232.         } else if (strncmp("ppp", device, 3) == 0) {
  233.                 p->linktype = DLT_PPP;
  234.         } else {
  235.                 sprintf(ebuf, "Spy: unknown physical layer type");
  236.                 goto bad;
  237.         }
  238.  
  239.         p->bufsize = 4096;                              /* XXX */
  240.         p->buffer = (u_char *)malloc(p->bufsize);
  241.         if (p->buffer == NULL) {
  242.                 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
  243.                 goto bad;
  244.         }
  245.  
  246.         return (p);
  247. bad:
  248.         strcpy(IpSpy_Device, "");
  249.         IpSpy_Handle = 0;
  250.         if ((rc = IpSpy_Exit(ulHandle)) != RC_IPSPY_NOERROR)
  251.           fprintf(stderr, "IpSpy Error IpSpy_Exit(): %d\n", rc);
  252.         free(p);
  253.         return (NULL);
  254. }
  255.  
  256. int
  257. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  258. {
  259.  
  260.         p->fcode = *fp;
  261.         return (0);
  262. }
  263.  
  264. void
  265. pcap_close(pcap_t *p)
  266. {
  267.         APIRET rc;
  268.  
  269.         if (p->fd >= 0) {
  270.           if ((rc = IpSpy_Exit(p->fd)) != RC_IPSPY_NOERROR)
  271.             fprintf(stderr, "IpSpyExit Error: %d\n", rc);
  272.  
  273.           if ((rc = IpSpy_SetReceiveMode(IpSpy_OldMode, IpSpy_Device, NULL)) != RC_IPSPY_NOERROR)
  274.             fprintf(stderr, "IpSpy_SetReceiveMode Error: %d\n", rc);
  275.  
  276.           strcpy(IpSpy_Device, "");
  277.           IpSpy_Handle = 0;
  278.  
  279.           fprintf(stderr, "IpSpy terminated\n");
  280.         }
  281.         if (p->sf.rfile != NULL) {
  282.                 (void)fclose(p->sf.rfile);
  283.                 if (p->sf.base != NULL)
  284.                         free(p->sf.base);
  285.         } else if (p->buffer != NULL)
  286.                 free(p->buffer);
  287.  
  288.         free(p);
  289. }
  290.